package com.kedou.sdk.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.kedou.sdk.R;
import com.kedou.sdk.core.HttpEngineApi;
import com.kedou.sdk.core.HttpEngineConduits;
import com.kedou.sdk.ui.assist.ThirdLoginUtil;
import com.kedou.sdk.utils.ClickUtil;
import com.kedou.sdk.utils.ResUtils;
import com.squareup.picasso.Picasso;

/**
 * Created by Administrator on 2018/3/7.
 */

public class LoginDialogFragment2 extends BaseDialogFragment implements View.OnClickListener {

    private static final String INIT_LOGIN_TYPE = "init_login_type";

    private ImageButton mCloseDialogBtn;
    private ImageView logoImage;
    private Button mOneKeyLoginBtn;
    private Button loginMoreBtn;



    public static final LoginDialogFragment2 newInstance() {
        LoginDialogFragment2 fragment = new LoginDialogFragment2();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View contentView = doInflateContentView(inflater, container, getLayoutIdentifier("wan_activity_login2"));



        mOneKeyLoginBtn = contentView.findViewById(getViewIdentifier("activity_login_onekey_register_btn"));
        loginMoreBtn = contentView.findViewById(getViewIdentifier("activity_login_more"));
        mCloseDialogBtn = contentView.findViewById(getViewIdentifier("activity_login_finish_btn"));
        logoImage = contentView.findViewById(getViewIdentifier("login_logo"));
        HttpEngineConduits wanSDKDataDao = HttpEngineApi.getInstance().getEngineConduits();
        Log.e("a",wanSDKDataDao.getPlatformConfigInfo().getBox_logo());
        Picasso.with(getContext()).load(wanSDKDataDao.getPlatformConfigInfo().getBox_logo()).error(ResUtils.getDrawableIdentifier(getContext(),"wan_icon_hezi_icon")).into(logoImage);

        loginMoreBtn.setVisibility(ThirdLoginUtil.getInstance().isAppInstalled(getContext())?View.GONE:View.VISIBLE);
        loginMoreBtn.setOnClickListener(this);
        mOneKeyLoginBtn.setOnClickListener(this);
        mCloseDialogBtn.setOnClickListener(this);



        return contentView;
    }

    private LoginDialogFragment.OnUserActionListener getOnUserActionListener() {
        if (!isNullContext()) {
            Activity activity = getActivity();
            if (activity instanceof LoginDialogFragment.OnUserActionListener) {
                return (LoginDialogFragment.OnUserActionListener) activity;
            }
        }
        return null;
    }

    @Override
    public void onClick(View view) {
        if (view == mCloseDialogBtn) {
            finishActivity();
        } else if (view == mOneKeyLoginBtn) {
            if(ClickUtil.isFastClick()){
                nextToAppActivity();
            }
//                mOneKeyLoginBtn.setEnabled(false);
//                mOneKeyLoginBtn.setClickable(false);
        }else if(view == loginMoreBtn){
            mOneKeyLoginBtn.setEnabled(false);
            mOneKeyLoginBtn.setClickable(false);
            nextToLoginMoreActivity();
        }
    }

    private void nextToAppActivity() {
        LoginDialogFragment.OnUserActionListener listener = getOnUserActionListener();
        if (listener != null) {
            listener.doNextToAppLogin();
        }
    }
    private void nextToLoginMoreActivity() {
        LoginDialogFragment.OnUserActionListener listener = getOnUserActionListener();
        if (listener != null) {
            listener.doNextToLoginMore();
        }
    }

    private void finishActivity() {
        LoginDialogFragment.OnUserActionListener listener = getOnUserActionListener();
        if (listener != null) {
            listener.doFinishActivity(this);
        }
    }




}
